package org.jeecg.modules.demo.yanhao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.yanhao.entity.Evaluate;
import org.jeecg.modules.demo.yanhao.entity.YhBasic;
import org.jeecg.modules.demo.yanhao.mapper.EvaluateMapper;
import org.jeecg.modules.demo.yanhao.mapper.YhBasicMapper;
import org.jeecg.modules.demo.yanhao.service.IYhBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 验号基本信息
 * @Author: jeecg-boot
 * @Date:   2020-09-17
 * @Version: V1.0
 */
@Service
public class YhBasicServiceImpl extends ServiceImpl<YhBasicMapper, YhBasic> implements IYhBasicService {

	@Autowired
	private YhBasicMapper yhBasicMapper;
	@Autowired
	private EvaluateMapper evaluateMapper;
	
	@Override
	@Transactional
	public void saveMain(YhBasic yhBasic, List<Evaluate> evaluateList) {
		yhBasicMapper.insert(yhBasic);
		if(evaluateList!=null && evaluateList.size()>0) {
			for(Evaluate entity:evaluateList) {
				//外键设置
				entity.setWangwang(yhBasic.getWangwang());
				evaluateMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(YhBasic yhBasic, List<Evaluate> evaluateList) {
		yhBasicMapper.updateById(yhBasic);

		//1.先删除子表数据
		evaluateMapper.deleteByWangwang(yhBasic.getWangwang());

		//2.子表数据重新插入
		if(evaluateList!=null && evaluateList.size()>0) {
			for(Evaluate entity:evaluateList) {
				//外键设置
				entity.setWangwang(yhBasic.getWangwang());
				evaluateMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		YhBasic yhBasic = yhBasicMapper.selectById(id);
		evaluateMapper.deleteByWangwang(yhBasic.getWangwang());
		yhBasicMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			YhBasic yhBasic = yhBasicMapper.selectById(id);
			evaluateMapper.deleteByWangwang(yhBasic.getWangwang());
			yhBasicMapper.deleteById(id);
		}
	}


}
